package com.lz.football.module.guide.setting.view
{
	import com.common.stdlib.ArrayUtil;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.GuideEvent;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.lz.football.module.guide.item.GuideTeamIconItem;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import lz.controls.PageComponent;
	import lz.controls.events.PageEvent;

	/**
	 *队徽选择 
	 * @author lake
	 * 
	 */	
	public class TeamIconSettingPanel extends GuideBaseSelectPanel
	{
		/**
		 *分页 
		 */		
		private var pageComp :PageComponent ;
		/**
		 *队徽item 列表 
		 */		
		private var itemList :Array = [];
		/**
		 *队徽xml描述 
		 */		
		private var teamList :Array = [];
		/**
		 *选择的item 
		 */		
		private var selectItem :GuideTeamIconItem;
		
		public function TeamIconSettingPanel()
		{
			super();
		}
		/**
		 * 初始化
		 * @param view
		 * 
		 */		
		override public function initUI(view:Object=null):void
		{
			_symbolSkin = view as MovieClip;
			nextBtn = _symbolSkin.nextBtn;
			container = _symbolSkin.container;
			pageComp = new PageComponent();
			pageComp.initUI(_symbolSkin.pageComp);
			var i :int = 0;
			var guideTeamIconItem :GuideTeamIconItem;
			for(;i<6;i++)
			{
				guideTeamIconItem = new GuideTeamIconItem();
				guideTeamIconItem.initUI();
				container.addChild(guideTeamIconItem);
				guideTeamIconItem.x = i * 141;
				itemList.push(guideTeamIconItem);
			}
			addEvtListener();
			initDatas();
		}
		/**
		 *初始化数据 
		 * 
		 */		
		private function initDatas():void
		{
			var teamIconConfig :XML = ResourceManager.instance.getResource(ResourceConst.CLUB_TEAM_ICON);
			if(teamIconConfig)
			{
				teamList = [];
				for each(var item :XML in teamIconConfig.item)
				{
					if(String(item.iconId[0]))
					{
						teamList.push(item);
					}
				}
				pageComp.totalNumbers = teamList.length;
				pageComp.pageSize = 6;
				pageComp.curPage = 1;
			}
		}
		/**
		 *添加事件 
		 */		
		override public function addEvtListener():void
		{
			super.addEvtListener();
			pageComp.addEventListener(PageEvent.PAGE_CHANGE,pageChangeHandler);
			AppEventDispatcher.getInstance().addEventListener(NotifierEvent.RES_CONFIG_LOAD_COMPLETE,resCompleteHandler);
			container.addEventListener("GuideTeamIconItem_select",selectHandler);
		}
		/**
		 *队徽选择 
		 * @param e
		 * 
		 */		
		private function selectHandler(e :Event):void
		{
			if(selectItem)
			{
				selectItem.unSelect();
			}
			selectItem = e.target as GuideTeamIconItem;
			selectItem.select();
			
		}
		/**
		 *数据准备完毕 
		 */		
		private function resCompleteHandler(e :Event):void
		{
			initDatas();
		}
		/**
		 *页数改变 
		 * @param e
		 * 
		 */		
		private function pageChangeHandler(e :PageEvent):void
		{
			setTeamIconDatas(ArrayUtil.getPageSizeDatas(pageComp.curPage,pageComp.pageSize,teamList));
		}
		/**
		 *设置队徽数据 
		 * @param datas
		 * 
		 */		
		public function setTeamIconDatas(datas :Array):void
		{
			var guideTeamIconItem :GuideTeamIconItem;
			var i :int = 0;
			for each(var dataObj :* in datas)
			{
				if(i >= 6)break;
				guideTeamIconItem = itemList[i];
				guideTeamIconItem.data = dataObj;
				i ++;
			}
			if(selectItem)
			{
				selectItem.unSelect();
			}
			selectItem = itemList[0];
			selectItem.select();
		}
		/**
		 *下一步 
		 * @param e
		 * 
		 */		
		override protected function nextHandler(e :MouseEvent):void
		{
			AppEventDispatcher.getInstance().dispatchEvent(new GuideEvent(GuideEvent.TEAMICON_SELECT,int(selectItem.data..iconId[0])));
		}
		/**
		 *淡入 
		 * 
		 */		
		override public function fadeIn():void
		{
			super.fadeIn();
			pageComp.curPage = 1;
		}
		/**
		 *删除事件 
		 * 
		 */		
		override public function removeEvtListener():void
		{
			super.removeEvtListener();
			pageComp.removeEventListener(PageEvent.PAGE_CHANGE,pageChangeHandler);
			AppEventDispatcher.getInstance().removeEventListener(NotifierEvent.RES_CONFIG_LOAD_COMPLETE,resCompleteHandler);
			container.removeEventListener("GuideTeamIconItem_select",selectHandler);
		}
	}
}